package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.domain.OrderDetail;
import com.example.reggie.domain.Orders;
import com.example.reggie.domain.User;
import com.example.reggie.dto.OrderDto;
import com.example.reggie.service.OrderDetailService;
import com.example.reggie.service.OrdersService;
import com.example.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("添加订单成功！");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(Integer page, Integer pageSize) {
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(userId != null, Orders::getUserId, userId);
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);

        Page<Orders> ordersPage = new Page<>(page, pageSize);
        ordersService.page(ordersPage, ordersLambdaQueryWrapper);
        log.info(ordersPage.getRecords().toString());
        Page<OrderDto> orderDtoPage = new Page<>();
        BeanUtils.copyProperties(ordersPage, orderDtoPage, "records");
        List<Orders> ordersPageRecords = ordersPage.getRecords();
        List<OrderDto> orderDtoPageRecords = new ArrayList<>();

        for (Orders ordersPageRecord : ordersPageRecords) {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(ordersPageRecord, orderDto);
            Long ordersId = Long.valueOf(ordersPageRecord.getNumber());
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, ordersId);
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailLambdaQueryWrapper);
            Integer sumNum = 0;
            for (OrderDetail detail : orderDetailList) {
                sumNum += detail.getNumber();
            }
            orderDto.setOrderDetails(orderDetailList);
            orderDto.setSumNum(sumNum);
            orderDtoPageRecords.add(orderDto);
        }
        orderDtoPage.setRecords(orderDtoPageRecords);
        return R.success(orderDtoPage);
    }

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, Long number, String beginTime, String endTime) {
        Page<OrderDto> orderDtoPage = new Page<>();
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(number != null, Orders::getNumber, number);
        ordersLambdaQueryWrapper.ge(beginTime != null, Orders::getOrderTime, beginTime);
        ordersLambdaQueryWrapper.le(endTime != null, Orders::getOrderTime, endTime);
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, ordersLambdaQueryWrapper);
        BeanUtils.copyProperties(ordersPage, orderDtoPage, "records");

        List<OrderDto> orderDtoPageRecords = new ArrayList<>();
        List<Orders> ordersPageRecords = ordersPage.getRecords();

        for (Orders ordersPageRecord : ordersPageRecords) {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(ordersPageRecord, orderDto);
            Long userId = ordersPageRecord.getUserId();
            User user = userService.getById(userId);
            String userName = user.getName();
            orderDto.setUserName(userName);
            orderDtoPageRecords.add(orderDto);
        }

        orderDtoPage.setRecords(orderDtoPageRecords);


        return R.success(orderDtoPage);
    }

    @PutMapping
    public R<String> put(@RequestBody Orders orders) {
        LambdaUpdateWrapper<Orders> ordersLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        ordersLambdaUpdateWrapper.set(orders.getStatus() != null, Orders::getStatus, orders.getStatus());
        ordersService.updateById(orders);

        return R.success("更新状态成功！");
    }

    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        ordersService.again(orders);
        return R.success("再来一单功能成功！");
    }


}
